#include "oskolok.h"

extern int G_W,G_H;

OSKOLOK::OSKOLOK(int num, int speed, SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    qsrand(QTime::currentTime().second() );

    rotate=0+qrand()%(360-0+1);

    this->num=num;

    point=15;

    border = cash->cash_poly->value("Res/weapons/Oskolki/"+QString::number(num)+".poly");
    im = cash->cash_pixmap->value("Res/weapons/Oskolki/"+QString::number(num)+".png");

    this->setZValue(0.5);

    QPoint *start = new QPoint( 0+qrand()%(G_W-0+1) ,-100 );

    this->setPos(*start);

    anim_rocket = new QGraphicsItemAnimation();
    rocket_time_line = new QTimeLine(speed);
    rocket_time_line->setEasingCurve(QEasingCurve::Type(1));
    rocket_time_line->setFrameRange(0,1);
    anim_rocket->setTimeLine(rocket_time_line);
    anim_rocket->setItem(this);

    anim_rocket->setPosAt(0,*start);
    anim_rocket->setPosAt(1,QPoint((start->x()),G_H+100));
    rocket_time_line->start();

    timer_id=startTimer(100);

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(rocket_time_line,SIGNAL(finished()),SLOT(deleteLater()));

}

QPainterPath OSKOLOK::shape() const
{

    return *border;

}

QRectF OSKOLOK::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );

}

void OSKOLOK::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

void OSKOLOK::timerEvent(QTimerEvent *)
{

    this->setRotation(++rotate);

}

OSKOLOK::~OSKOLOK()
{

    killTimer(timer_id);

}

int OSKOLOK::type() const
{

    return point;

}

void OSKOLOK::Damage_Detected(QPointF a, int d)
{

    a = QPointF();
    point-=d;

    if(point<=0) this->scene()->removeItem(this);

}




